package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryAchievementGradeByEntityReq;
import com.sg.dto.base.app.req.QueryAchievementGradeDetailReq;
import com.sg.dto.base.app.req.QueryAchievementGradeListReq;
import com.sg.entity.OmsAchievementGrade;
import com.sg.service.base.app.MOmsAchievementGradeService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsAchievementGradeMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAchievementGradeServiceImpl
        extends ServiceImpl<OmsAchievementGradeMapper, OmsAchievementGrade>
        implements MOmsAchievementGradeService {

    @Autowired
    OmsAchievementGradeMapper omsAchievementGradeMapper;

    /**
     * 3-10-04新增成果等级[9110]
     * gen by moon at 4/3/2024, 1:43:28 PM
     */
    @Trace(operationName = "3-10-04新增成果等级")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addAchievementGrade(OmsAchievementGrade omsAchievementGrade) {
        omsAchievementGrade.setAchievementGradeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAchievementGrade.setIsArchive("FALSE");
        omsAchievementGrade.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAchievementGrade.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievementGrade != null && omsAchievementGrade.getSubjectLifeCycle() != null && omsAchievementGrade.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementGrade.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsAchievementGrade.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsAchievementGrade.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievementGrade != null && omsAchievementGrade.getSubjectLifeCycle() != null && omsAchievementGrade.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementGrade.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAchievementGrade.setIsValid("FALSE");
        if (super.save(omsAchievementGrade)) {
            return omsAchievementGrade.getAchievementGradeId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-10-04删除成果等级[9112]
     * gen by moon at 4/3/2024, 1:43:29 PM
     */
    @Trace(operationName = "3-10-04删除成果等级")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAchievementGrade(String achievementGradeId) {

        return super.removeById(achievementGradeId);
    }

    /**
     * 3-10-04查询成果等级列表[9113]
     * gen by moon at 4/3/2024, 6:42:46 PM
     */
    @Trace(operationName = "3-10-04查询成果等级列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievementGrade> queryAchievementGradeList(QueryAchievementGradeListReq reqDto) {

        try {
            QueryWrapper<OmsAchievementGrade> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievementGrade.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievementGrade.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievementGrade.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsAchievementGrade>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-04查询成果等级详情[9115]
     * gen by moon at 4/3/2024, 1:43:36 PM
     */
    @Trace(operationName = "3-10-04查询成果等级详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsAchievementGrade queryAchievementGradeDetail(QueryAchievementGradeDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsAchievementGrade> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsAchievementGrade> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsAchievementGrade retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-04批量查询成果等级By主键[9167]
     * gen by moon at 4/3/2024, 6:42:47 PM
     */
    @Trace(operationName = "3-10-04批量查询成果等级By主键")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievementGrade> batchQueryAchievementGradeByEntity(BatchQueryAchievementGradeByEntityReq reqDto) {

        try {
            QueryWrapper<OmsAchievementGrade> queryWrapper = new QueryWrapper<>();

            if (reqDto.getAchievementGradeList() == null || reqDto.getAchievementGradeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("achievement_grade_id", reqDto.getAchievementGradeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievementGrade.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievementGrade.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievementGrade.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsAchievementGrade>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-04修改成果等级[9111]
     * gen by moon at 4/3/2024, 6:42:45 PM
     */
    @Trace(operationName = "3-10-04修改成果等级")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAchievementGrade(OmsAchievementGrade omsAchievementGrade) {
        omsAchievementGrade.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievementGrade != null && omsAchievementGrade.getSubjectLifeCycle() != null && omsAchievementGrade.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementGrade.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsAchievementGrade != null && omsAchievementGrade.getSubjectLifeCycle() != null && omsAchievementGrade.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievementGrade.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsAchievementGrade.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievementGrade != null && omsAchievementGrade.getSubjectLifeCycle() != null && omsAchievementGrade.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementGrade.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsAchievementGrade != null && omsAchievementGrade.getSubjectLifeCycle() != null && omsAchievementGrade.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievementGrade.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsAchievementGrade);
    }
}
